// pages/index/recommendMore/recommendMore.js
const tools=require('../../../utils/tool')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:0,
    recommend:[]
  },
 
  onLoad: function (options) {
    this.setData({
      second_cate_id:options.second_cate_id
    })
   
  },
// 获取推荐更多
  getRecommendMore(url,second_cate_id){
    tools.showLoading()
    let getMore=tools.request(url,'get',{
      cateId:second_cate_id,
      page:this.data.page,
      size:10,
    },wx.getStorageSync('token'))
    getMore.then(res=>{
      console.log(res,12255)
      let recommend=this.data.recommend.concat(res.data.content)
      wx.hideLoading()
      if(res.statusCode==200){
        this.setData({
          recommend,
          page:this.data.page+1,
          totalElements:res.data.totalElements
        })
      }else{
        wx.showToast({
          title: res.data.error_msg,
          icon:"none"
        })
      }
      console.log(res)
    })
    .catch(err=>{
     console.log(err)
    })
  },
 
  onReachBottom: function () {
      console.log(this.data.totalElements)
      if(this.data.recommend.length<this.data.totalElements){
        this.getRecommendMore('/aty/applet/a/article/new',this.data.second_cate_id)
      }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShow(){
    this.setData({
      page:0,
      recommend:[]
    })
    this.getRecommendMore('/aty/applet/a/article/new',this.data.second_cate_id)
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    let json = currPage.data.mydata;
    if(typeof json!='undefined'){
      if(json.isZan){
        let recommend = this.data.recommend
        recommend.forEach((item, i) => {
          if (json.id == recommend[i].id) {
            this.setData({
              [`recommend[${i}].is_good`]: json.state
            })
          }
        })
       
      }
    }
  }
})